#include <string.h>
#include <iostream>

using std::cout;
using std::endl;

class String
{
public:
    String()
    : _pstr(nullptr)
    {
        cout << "String()" << endl;
    }

    String(const char *pstr)
    : _pstr(new char[strlen(pstr) + 1]())
    {
        cout << "String(const char *)" << endl;
        strcpy(_pstr, pstr);
    }

    //将拷贝构造函数以及赋值运算符函数称为具有复制控制语义的函数
    String(const String &rhs)
    : _pstr(new char[strlen(rhs._pstr) + 1]())
    {
        cout << "String(const String &)" << endl;
        strcpy(_pstr, rhs._pstr);
    }

    String &operator=(const String &rhs)
    {
        cout << "String &operator=(const String &)" << endl;
        //1、自复制
        if(this != &rhs)
        {
            //2、释放左操作数
            delete [] _pstr;
            _pstr = nullptr;
            
            //3、深拷贝
            _pstr = new char[strlen(rhs._pstr) + 1]();
            strcpy(_pstr, rhs._pstr);
        }

        //4、返回*this
        return  *this;
    }

    //将移动构造函数以及移动赋值运算符函数称为具有移动语义的函数
    //
    //1、移动语义的函数优先于具有拷贝语义的函数的执行
    //2、具有移动语义的函数如果不写的话，编译器是不会自动生成，
    //必须手写
    //
    //移动构造函数
    //String s4 = String("world")
    String(String &&rhs)
    : _pstr(rhs._pstr)
    {
        cout << "String(String &&)" << endl;
        rhs._pstr = nullptr;
    }

    //移动赋值函数（移动赋值运算符函数）
    String &operator=(String &&rhs)
    {
        cout << "String &operator=(String &&)" << endl;
        //1、自移动(自己移动给自己)
        if(this != &rhs)
        {
            //2、释放左操作数
            delete [] _pstr;
            _pstr = nullptr;
            
            //3、浅拷贝
            _pstr = rhs._pstr;
            rhs._pstr = nullptr;
        }

        //4、返回*this
        return  *this;
    }

    ~String()
    {
        cout << "~String()" << endl;
        if(_pstr)
        {
            delete [] _pstr;
            _pstr = nullptr;
        }
    }

    friend std::ostream &operator<<(std::ostream &os, const String &rhs);
private:
    char *_pstr;
};

std::ostream &operator<<(std::ostream &os, const String &rhs)
{
    if(rhs._pstr)
    {
        os << rhs._pstr;
    }

    return os;
}

void test()
{
    String s1("hello");
    cout << "s1 = " << s1 << endl;

    cout << endl;
    String s2 = s1;
    cout << "s1 = " << s1 << endl;
    cout << "s2 = " << s2 << endl;

    cout << endl;
    //从C风格字符串向C++风格字符串的转换
    //C++        C
    //"hello"===String("hello")
    String s3 = "hello";//隐式转换,靠构造函数做支撑
    cout << "s3 = " << s3 << endl;
    /* &"hello";//C风格字符串,左值,可以进行取地址的，位于文字常量区 */
    /* &String("hello");//C++风格字符串,右值，临时对象 */

    cout <<endl;
    String s4 = String("world");
    cout << "s4 = " << s4 << endl;

    cout << endl;
    s4 = s1;
    cout << "s1 = " << s1 << endl;
    cout << "s4 = " << s4 << endl;

    cout << endl;
    s4 = String("wangdao");
    cout << "s4 = " << s4 << endl;

    cout << endl << "执行std::move函数" << endl;
    /* String("hello") = String("hello"); */
    //std::move作用就是可以将一个左值转换为右值,std::move的本质
    //并不是像函数名字一样，做了移动,就是底层做了一个强制转换
    //static_cast<T &&>(lvalue)
    s1 = std::move(s1);
    cout << "s1 = " << s1 << endl;
}

int main(int argc, char **argv)
{
    test();
    return 0;
}

